home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-06-04 | 5.6 KB | 238 lines | [TEXT/MPS ] |
- /* File: Recorder.c
-
- MPW Tool for recording serial line traffic
-
- */
-
-
- #include <Types.h>
- #include <QuickDraw.h>
- #include <CursorCtl.h>
-
- #include <CRMIntf.h>
- #include <CTBUtils.h>
- #include <CMIntf.h>
- #include <FTIntf.h>
- #include <TMIntf.h>
-
- #include <StdIO.h>
- #include <StdLib.h>
-
- pascal void SetupGlob ( void );
- pascal void ReadCompletionGlue1 ( ConnHandle hConn );
- pascal void ReadCompletionGlue2 ( ConnHandle hConn );
- pascal void WriteCompletionGlue1 ( ConnHandle hConn );
- pascal void WriteCompletionGlue2 ( ConnHandle hConn );
-
- #define CommToolBoxTrap 0x8B
- #define UnimplementedTrap 0x9F
- #define Check(err,str) { \
- OSErr errXYZZY; \
- if (( errXYZZY = ( err )) != noErr ) \
- fprintf ( stderr, "Error %d calling %s\n", errXYZZY, str ); \
- }
- #define OUTCONFIGSTR "Baud 2400 dataBits 8 Parity None StopBits 1 Port \"Modem Port\"" \
- "Handshake None HoldConnection False RemindDisconnect False"
- #define INCONFIGSTR "Baud 2400 dataBits 8 Parity None StopBits 1 Port \"Printer Port\"" \
- "Handshake None HoldConnection False RemindDisconnect False"
-
- #define BUF_SIZE 1024
-
- short procID1;
- short procID2;
- ConnHandle stream1;
- ConnHandle stream2;
- char buffer1 [ BUF_SIZE ];
- char buffer2 [ BUF_SIZE ];
- long readSize1;
- long writeSize1;
- long readSize2;
- long writeSize2;
- CMFlags inFlags1;
- CMFlags inFlags2;
- CMFlags outFlags1;
- CMFlags outFlags2;
-
- /* Is the Comm Toolbox actually installed ?? */
- Boolean IsCTBInstalled ( ) {
- return NGetTrapAddress ( UnimplementedTrap, OSTrap ) !=
- NGetTrapAddress ( CommToolBoxTrap, OSTrap );
- }
-
- short InitAll ( void ) {
- OSErr err;
-
- InitGraf ( &qd.thePort );
- /*
- InitFonts ();
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs ( NULL );
- InitCursor ();
- */
-
- InitCursorCtl ( NULL );
- SetupGlob ();
-
- if ( !IsCTBInstalled ) {
- fprintf ( stderr, "Comm Toolbox not installed!\n" );
- return 1;
- }
-
- /* Load up the Communications Toolbox */
- (void) InitCTBUtilities ();
- (void) InitCRM ();
-
- err = InitTM ();
- if ( err == tmNoTools ) {
- fprintf ( stderr, "No terminal tools found\n" );
- return 2;
- }
-
- err = InitCM ();
- if ( err == cmNoTools ) {
- fprintf ( stderr, "No connection tools found\n" );
- return 2;
- }
-
- err = InitFT ();
- if ( err == ftNoTools ) {
- fprintf ( stderr, "No file transfer tools found\n" );
- return 2;
- }
-
- return 0;
- }
-
-
- void ExitProc ( void ) {
- DebugStr ( "\pExit" );
-
- /* Close the connection and dispose of the connection record */
- if ( stream1 != NULL ) {
- Check ( CMClose ( stream1, false, NULL, -1, true ), "CMCLose - input" );
- CMDispose ( stream1 );
- }
-
- if ( stream2 != NULL ) {
- Check ( CMClose ( stream2, false, NULL, -1, true ), "CMCLose - output" );
- CMDispose ( stream2 );
- }
- }
-
- ConnHandle InitStream ( short *procID ) {
- CMBufferSizes bSize;
-
- /* Open a connection tool */
- bSize [ cmDataIn ] = BUF_SIZE; bSize [ cmDataOut ] = BUF_SIZE;
- bSize [ cmCntlIn ] = 0; bSize [ cmCntlOut ] = 0;
- bSize [ cmAttnIn ] = 0; bSize [ cmAttnOut ] = 0;
- bSize [ cmRsrvIn ] = 0; bSize [ cmRsrvOut ] = 0;
- *procID = CMGetProcID ( "\pSerial" );
- return CMNew ( *procID, cmQuiet + cmNoMenus, bSize, 0L, 0L );
- }
-
-
- /* Interrupt routine */
- pascal void ReadCompletion1 ( ConnHandle hConn ) {
-
- if ((*hConn)->errCode == noErr ) {
- writeSize1 = (*hConn)->asyncCount [ cmDataIn ];
- CMWrite ( stream2, buffer1, &writeSize1,
- cmData, true, (ProcPtr) WriteCompletionGlue1, 0, inFlags1 );
- }
- }
-
-
- /* Interrupt routine */
- pascal void WriteCompletion1 ( ConnHandle hConn ) {
-
- /* re-enable the read */
- if ((*hConn)->errCode == noErr ) {
- readSize1 = 1;
- CMRead ( stream1, buffer1, &readSize1,
- cmData, true, (ProcPtr) ReadCompletionGlue1, 0, &inFlags2 );
- }
- }
-
-
- /* Interrupt routine */
- pascal void ReadCompletion2 ( ConnHandle hConn ) {
-
- if ((*hConn)->errCode == noErr ) {
- writeSize2 = (*hConn)->asyncCount [ cmDataIn ];
- CMWrite ( stream1, buffer2, &writeSize2,
- cmData, true, (ProcPtr) WriteCompletionGlue2, 0, inFlags2 );
- }
- }
-
-
- /* Interrupt routine */
- pascal void WriteCompletion2 ( ConnHandle hConn ) {
-
- /* re-enable the read */
- if ((*hConn)->errCode == noErr ) {
- readSize2 = 1;
- CMRead ( stream2, buffer2, &readSize2,
- cmData, true, (ProcPtr) ReadCompletionGlue2, 0, &inFlags1 );
- }
- }
-
-
- int main ( int argc, char *argv[] ) {
- #pragma unused ( argc )
- #pragma unused ( argv )
- short err;
- long cnt;
-
- if ( err = InitAll ( ) != 0 )
- exit ( err );
-
- atexit ( ExitProc );
-
- /* Open a connection tool */
- stream1 = InitStream ( &procID1 );
- if ( stream1 == NULL ) {
- fprintf ( stderr, "Cannot create input handle\n" );
- return 3;
- }
-
- /* Open another connection tool */
- stream2 = InitStream ( &procID1 );
- if ( stream2 == NULL ) {
- fprintf ( stderr, "Cannot create output handle\n" );
- return 3;
- }
-
- /* Configure the connection */
- Check ( CMSetConfig ( stream1, INCONFIGSTR ), "CMSetConfig - In" );
- Check ( CMOpen ( stream1, false, NULL, -1 ), "CMOpen - In" );
- Check ( CMListen ( stream1, false, NULL, -1 ), "CMListen - In" );
-
- Check ( CMSetConfig ( stream2, OUTCONFIGSTR ), "CMSetConfig - Out" );
- Check ( CMOpen ( stream2, false, NULL, -1 ), "CMOpen - Out" );
- Check ( CMListen ( stream2, false, NULL, -1 ), "CMListen - Out" );
-
- /* Do an ansych read on the input stream */
- readSize1 = 1;
- Check ( CMRead ( stream1, buffer1, &readSize1,
- cmData, true, (ProcPtr) ReadCompletionGlue1, 0, &inFlags1 ), "CMRead" );
- readSize2 = 1;
- Check ( CMRead ( stream2, buffer2, &readSize2,
- cmData, true, (ProcPtr) ReadCompletionGlue2, 0, &inFlags2 ), "CMRead" );
- cnt = 0;
- while ( true ) {
-
- cnt++;
- if ( cnt % 256 == 0 ) {
- CMIdle ( stream1 );
- CMIdle ( stream2 );
- SpinCursor ( 1 );
- }
-
- }
-
- return 0;
- }
-